<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
    <a-form :form="form">
      <a-form-item :xs="24" :sm="21">
        <div style="display: flex">
          <a-select
            placeholder="请选择部门"
            v-model="departNames"
            mode="multiple"
            :disabled="disabled"
            style="margin-top: 3px;"
          />
          <a-button @click="selectDepart" style="margin-left: 10px;margin-top: 3px;">选择</a-button>
        </div>
      </a-form-item>
      <oa-offcialdoc-select-depart-modal
        ref="departModal"
        :multi="true"
        @ok="resultOk">
      </oa-offcialdoc-select-depart-modal>
    </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import { httpAction, postAction,getAction  } from '@/api/manage'
  import oaOffcialdocSelectDepartModal from './OaOffcialdocSelectDepartModal'
  export default {
    name: "OaOfficialdocDistributeModal",
    components: {
      JSelectDepart,
      oaOffcialdocSelectDepartModal
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:'转发',
        width:1000,
        visible: false,
        disableSubmit: false,
        loading: false,
        confirmLoading:false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        customReturnField:"id",
        validatorRules: {
          departId: {
            rules: [
              { required: true, message: '请选择转发部门!' }
            ]
          }
        },
        url: {
          forward: "/eoa/officialdoc/oaOfficialdocDistribute/forward",
          startProcess: '/act/process/extActProcess/startMutilProcess',
          updateStatus:"/eoa/officialdoc/oaOfficialdocDistribute/updateStatus",
          queryPageList: "/eoa/officialdoc/oaOfficialdocDepartUser/list",
        },
        flowCode: 'dev_oa_officialdoc_received_001',
        type:false, //批量转发还是单独转发
        disabled:true,
        departIds:"",
        departNames:[],
        newDeparts:[]
      }
    },
    methods: {
      edit (record) {
        this.model=record
        this.type=record.type
        getAction(this.url.queryPageList).then((res)=>{
          if(res.success){
            var userDepart=res.result.records
            let departIds="";
            if(userDepart){
              for (let i = 0; i <userDepart.length ; i++) {
                let departId = userDepart[i].departId
                departIds+=departId+",";
              }
              if(departIds){
                this.newDeparts=departIds;
              }
            }
          }
        })
        this.visible=true
      },
      close () {
        this.departIds=""
        this.departNames=[]
        this.newDeparts=[]
        this.$emit('ok');
        this.visible = false;
      },
      handleOk () {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            // that.confirmLoading = true
            let method = 'put'
            let formData = Object.assign(this.model, values)
            formData.departId=this.departIds;
            if(this.type){
              let data={};
              data.ids=this.model.ids;
              data.departId=this.departIds
              httpAction(this.url.updateStatus,data,method).then((res) =>{
                if(res.success){
                  if(res.result){
                    let result = res.result.split(",")
                    console.log(result)
                    for (let i = 0; i <result.length ; i++) {
                      that.startProcess(result[i])
                    }
                    this.$message.success("转发成功！");
                    this.close();
                  }
                }else{
                  this.$message.warning("转发文失败！");
                }
              })
            }else{
              console.log(formData)
              httpAction(this.url.forward, formData, method).then((res) => {
                if (res.success) {
                  that.startProcess(res.result)
                  this.$message.success("转发成功！");
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.message)
                }
              }).finally(() => {
                that.confirmLoading = false
                that.close()
              })
            }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      startProcess(record) {
        console.log(record)
        let params = {
          flowCode: this.flowCode,
          id: record,
          formUrl: 'modules/eoa/officialdoc/modules/OaOfficialdocReceivedForm',
          formUrlMobile: 'modules/eoa/officialdoc/modules/OaOfficialdocReceivedForm'
        }
        postAction(this.url.startProcess, params).then(res => {
          if (res.success) {
          } else {
            this.$message.warning(res.message);
          }
        }).catch((e) => {
          this.$message.warning('不识别的请求!');
        })
      },
      selectDepart(){
        this.$refs.departModal.visible=true;
        var param=[];
        param.departIds=this.departIds;
        param.newDeparts=this.newDeparts;
        this.$refs.departModal.loadDepart(param,"");
      },
      resultOk(data,departIds){
        console.log(data)
        if(data){
          var departNames = data.map(data => data['departName']).join(',')
          this.departNames = departNames.split(",")
        }
        this.departIds=departIds;
      },
    }
  }
</script>